package com.sofwin.system.service.impl;

import com.sofwin.pojo.SysMenu;
import com.sofwin.pojo.SysMenuExample;
import com.sofwin.system.mapper.SysMenuMapper;
import com.sofwin.system.service.MenuService;
import com.sofwin.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.system.service.impl
 * @user: andyliu
 * @date: 2021/12/17 9:49
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private SysMenuMapper mapper;
    @Override
    public List<SysMenu> queryAllMenus(Integer roleId) {
        return mapper.selectRoleMenus(roleId);
    }

    @Override
    public List<MenuVO> queryMenusByRoleId(Integer roleId) {
        return mapper.selectMenusByRoleId(roleId);
    }

    @Override
    public List<SysMenu> selectMenus(SysMenu menu) {
        SysMenuExample example = new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        if(menu!=null){
            if(!StringUtils.isEmpty(menu.getMenuName())){
                criteria.andMenuNameLike("%"+menu.getMenuName()+"%");
            }
            if(menu.getStatus()!=null && menu.getStatus()!=-1){
                criteria.andStatusEqualTo(menu.getStatus());
            }

        }
        return mapper.selectByExample(example);
    }

    @Override
    public SysMenu selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveOrUpdate(SysMenu menu) {
        if(menu.getId()!=null){
           return mapper.updateByPrimaryKeySelective(menu)==1?true:false;
        }
        return mapper.insertSelective(menu)==1?true:false;
    }
}
